package com.atguigu.servlet;

import com.atguigu.utils.CookieUtils;

import javax.servlet.ServletException;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

public class CookieServlet extends BaseServlet {
    protected void creatCookie(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //1.创建Cookie对象
        Cookie cookie=new Cookie("key4","value4");
        //2.通知客户端保存Cookie
        resp.addCookie(cookie);

        //1.创建Cookie对象
        Cookie cookie1=new Cookie("key5","value5");
        //2.通知客户端保存Cookie
        resp.addCookie(cookie1);

        resp.getWriter().write("Cookie创建成功");
    }
    protected void getCookie(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        Cookie[] cookies=req.getCookies();

        //getName方法返回cookie的key(名)
        //getValue方法返回Cookie的value值
        for(Cookie cookie:cookies){
            resp.getWriter().write("Cookie["+cookie.getName()+"="+cookie.getValue()+"]<br/>");

        }

        Cookie iwantCookie=null;
        //getName方法返回cookie的key(名)
        //getValue方法返回Cookie的value值
//        for(Cookie cookie:cookies){
//            if("key3".equals(cookie.getName())){
//                iwantCookie=cookie;
//                break;
//            }
//        }
        iwantCookie= CookieUtils.findCookie("key1",cookies);
        //如果不等于null,说明赋过值，也就是找到了需要的Cookie
        if(iwantCookie!=null){
            resp.getWriter().write("找到了需要的Cookie");
        }

    }

    protected void UpdateCookie(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //方案一
        //1.先创建一个要修改的同名的Cookie对象
        //2.在构造器，同时赋予新的Cookie值
//        Cookie cookie=new Cookie("key1","newValue1");
        //3.调用response.addCookie(cookie)
//        resp.addCookie(cookie);
//        resp.getWriter().write("key1的Cookie已经修改好了");


        //方案二
        //1.先查找到需要修改的cookie对象
        Cookie cookie=CookieUtils.findCookie("key3",req.getCookies());
        if(cookie!=null){
            //2.调用setValue()方法赋予新的Cookie值
            cookie.setValue("newValue3");
            //3.调用response.addCookie()通知客户端保存修改
            resp.addCookie(cookie);
        }


        resp.getWriter().write("key3的Cookie已经修改好了");
    }
    protected void defaultLife(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        Cookie cookie=new Cookie("defaultLife","defaultLife");
        cookie.setMaxAge(-1);//设置存活时间
        resp.addCookie(cookie);
    }
    protected void deleteNow(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //先找到你要删除的Cookie对象
        Cookie cookie=CookieUtils.findCookie("key4",req.getCookies());
        if(cookie!=null){
            //调用setMaxAge(0);
            cookie.setMaxAge(0);//表示马上删除，都不需要等待浏览器关闭
            //调用response.addCookie(cookie)
            resp.addCookie(cookie);

            resp.getWriter().write("key4的cookie已经被删除");
        }

    }
    protected void life3600(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        Cookie cookie=new Cookie("life3600","life3600");
        cookie.setMaxAge(60*60);//设置Cookie一小时之后被删除。无效
        resp.addCookie(cookie);
        resp.getWriter().write("已经创建了一个存活一小时的cookie");
    }

    protected void testPath(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        Cookie cookie=new Cookie("path1","path1");
        //getContextPath() ===>>>得到工程路径
        cookie.setPath(req.getContextPath()+"/abc");//===>>> /工程路径/abc
        resp.addCookie(cookie);
        resp.getWriter().write("创建了一个带有Path路径的Cookie");
    }
}
